package com.webproject.shop.controller;

import com.webproject.shop.pojo.Product;
import com.webproject.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.webproject.shop.common.ResponseModel;

import java.util.List;

@Controller
@RequestMapping("/product")
@CrossOrigin(origins = "${webproject.web.path}", allowedHeaders = "*", allowCredentials = "true")

public class ProductController {
    @Autowired
    private ProductService productService;


    @RequestMapping(path = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel getProductList() {
        List<Product> products = productService.findAll();
        return new ResponseModel(products);
    }

    @RequestMapping(path = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel getProductDetail(@PathVariable("id") int id) {
        Product product = productService.findProductById(id);
        return new ResponseModel(product);
    }

    @RequestMapping(path = "/listbyname", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel getProductListByName(String name) {
        List<Product> products = productService.selectByName(name);
        return new ResponseModel(products);
    }


    @RequestMapping(path = "/listbyclass", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel getProductListByClass(String proClass) {
        List<Product> products = productService.selectByClass(proClass);
        return new ResponseModel(products);
    }



}
